<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserListForm extends CFormModel implements IMiniyunGridConfig
{
	public $searchKey;	public $action;	public $baseAction;
	public $baseActionArray = array(""=>"批量操作","disabled"=>"冻结","enabled"=>"解冻","delete"=>"删除");

	public $roleAction;
	public $roleActionArray = array(""=>"将角色变更为…","roleToAdmin"=>"管理员","roleToUser"=>"普通用户");

	public function rules()
	{
		return array(
		array('searchKey,action,baseAction,roleAction', 'length','max'=>128),
		); 
	}

	public function init(){
		parent::init();
		$this->baseAction="";
		$this->roleAction="";
	}
	
	public function getFormFilters(){
		return array(
             'isAdmin'=>array(
              "condition"=>"select user_id from ".Yii::app()->params['tablePrefix']."user_metas where meta_key='is_admin' and meta_value='1'"
              ),
              'disabled'=>array(
              "condition"=>"select id from ".Yii::app()->params['tablePrefix']."users where user_status=0"
              ),
              );
	}
	
	private function _getMultiNextActions(){
		return array(
            'baseAction'=>array(                "disabled"=>Yii::app()->createUrl("adminUserPanel/disabled"),
                "enabled"=>Yii::app()->createUrl("adminUserPanel/enabled"),
                "delete"=>Yii::app()->createUrl("adminUserPanel/delete")
		),
            'roleAction'=>array(                "roleToAdmin"=>Yii::app()->createUrl("adminUserPanel/changeRole&aim=admin"),
                "roleToUser"=>Yii::app()->createUrl("adminUserPanel/changeRole&aim=user"),
		),
		);
	}
	public function getSearchKey(){
		if($this->action=="search"){			$key = $this->searchKey;
			return "select id from ".Yii::app()->params['tablePrefix']."users where user_name like '%$key%' or id in(select user_id from ".Yii::app()->params['tablePrefix']."user_metas where meta_key in('nick','email','phone') and meta_value like '%$key%')";
		}
	}
	
	public function getDataProvider()
	{
		return new UserListDataProvider($this);
	}
	
	public function mutiAction(){
		$list = $this->_getMultiNextActions();
		if($this->action=="roleAction"){			if(isset($this->roleAction) && strlen($this->roleAction)>0){
				$url = $list["roleAction"][$this->roleAction];
			}
		}else if($this->action=="baseAction"){
			if(isset($this->baseAction) && strlen($this->baseAction)>0){
				$url = $list["baseAction"][$this->baseAction];
			}
		}
		if(isset($url)){
			$ids = Yii::app()->request->getParam("selectItems");
			if($ids!='' && $url!=''){
								$url = $url."&id=".implode(",", array_values($ids));;
				Yii::app()->getController()->redirect($url);
				return true;
			}
		}
		return false;
	}
}
